package com.tianye.salon.controller.admin;

import java.util.List;

import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.tianye.salon.exception.MyException;
import com.tianye.salon.model.AdminModule;
import com.tianye.salon.service.AdminModuleService;




/**
 * 功能菜单
 */
@Controller
@RequestMapping("/admin-module")
public class ModuleController {
	
	@Autowired
	private AdminModuleService adminModuleService;
	
	@RequestMapping("index")
	public ModelAndView index() throws Exception{
		List<AdminModule> modules = adminModuleService.selectAll();
		
		ModelAndView modelAndView = new ModelAndView("/admin/module");
		modelAndView.addObject("modules", modules);
		
		return modelAndView;
	}
	
	@RequestMapping("add")
	public ModelAndView add() {
		ModelAndView mav = new ModelAndView("/admin/module_add");
		
		return mav;
	}
	
	@RequestMapping("update")
	public ModelAndView update(int id,String pName,HttpSession session) throws Exception {
		//System.out.println("id is:"+id);
		//PtRole role = new PtRole();
		AdminModule module = adminModuleService.selectById(id);
		ModelAndView mav = new ModelAndView("/admin/module_update");
		mav.addObject("pName",pName);
		mav.addObject("module", module);
		
		return mav;
	}
	
	
	@RequestMapping(value="ajaxadd", produces="application/json; charset=utf-8")
	@ResponseBody
	public String add(int moduleId, String moduleName, String moduleLink, String moduleDescription, int moduleType, int modulePid0, HttpSession session) {
		int uid = Integer.parseInt(session.getAttribute("uid").toString());
		
		AdminModule module = new AdminModule();
		module.setId(moduleId);
		module.setName(moduleName);
		module.setLink(moduleLink);
		module.setDescription(moduleDescription);
		module.setType(moduleType);
		module.setStatus(0);
		module.setPid0(modulePid0);
		module.setSortNum(0);
		module.setCuid(uid);
		
		try {
			adminModuleService.insert(module);
		} catch (Exception e) {
			e.printStackTrace();
			return e.getMessage();
		}
		
		return "ok";
	}
	
	@RequestMapping(value="ajaxupdate", produces="application/json; charset=utf-8")
	@ResponseBody
	public String update(int moduleId, String moduleName, String moduleLink, String moduleDescription, int moduleType, int modulePid0, HttpSession session) throws Exception {
		int uid = Integer.parseInt(session.getAttribute("uid").toString());
		
		AdminModule module = adminModuleService.selectById(moduleId);
		
		try {
			if(null == module){
				throw new MyException("未找到待修改模块！");
			}
			
			module.setName(moduleName);
			module.setLink(moduleLink);
			module.setDescription(moduleDescription);
			module.setType(moduleType);
			module.setStatus(0);
			module.setPid0(modulePid0);
			module.setSortNum(0);
			module.setCuid(uid);
		
			adminModuleService.update(module);
		} catch (MyException e) {
			e.printStackTrace();
			return e.getMessage();
		}
		
		return "ok";
	}
	
	@RequestMapping(value="ajaxdelete", produces="application/json; charset=utf-8")
	@ResponseBody
	public String delete(int id) throws Exception{
		adminModuleService.deleteByPrimaryKey(id);
		
		return "ok";
	}
}
